package com.tyk.bookstore.back.platform.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tyk.bookstore.back.common.core.model.result.R;
import com.tyk.bookstore.back.platform.model.query.SystemSettingAddQuery;
import com.tyk.bookstore.back.platform.model.query.SystemSettingPageQuery;
import com.tyk.bookstore.back.platform.model.query.SystemSettingUpdateQuery;
import com.tyk.bookstore.back.platform.model.vo.SystemSettingPageVo;
import com.tyk.bookstore.back.platform.model.vo.SystemSettingVo;
import com.tyk.bookstore.back.platform.service.SystemSettingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * @author tyk
 * @description 系统参数
 */
@Tag(name = "平台管理/系统参数管理", description = "系统参数管理")
@RequiredArgsConstructor
@RestController
@RequestMapping("api/platform/setting")
public class SystemSettingController {

    private final SystemSettingService systemSettingService;

    @Operation(summary = "查询系统参数详情", description = "查询系统参数详情")
    @SaCheckPermission("platform:setting:get")
    @GetMapping("{id}")
    public R<SystemSettingVo> getById(
            @Schema(description = "系统参数ID")
            @NotNull @Min(value = 1)
            @PathVariable Long id
    ) {
        return R.ok(systemSettingService.getById(id));
    }

    @Operation(summary = "分页查询系统参数", description = "分页查询系统参数")
    @SaCheckPermission("platform:setting:page")
    @GetMapping("page")
    public R<SystemSettingPageVo> getPage(
            @Schema(description = "系统参数分页查询参数")
            @ParameterObject
            @Validated SystemSettingPageQuery query
    ) {
        return R.ok(systemSettingService.getPage(query));
    }

    @Operation(summary = "添加系统参数", description = "添加系统参数")
    @SaCheckPermission("platform:setting:add")
    @PostMapping
    public R<Object> add(
            @Validated @RequestBody SystemSettingAddQuery query
    ) {
        systemSettingService.add(query);
        return R.ok();
    }

    @Operation(summary = "更新系统参数", description = "更新系统参数")
    @SaCheckPermission("platform:setting:update")
    @PutMapping
    public R<Object> updateById(
            @Validated @RequestBody SystemSettingUpdateQuery query
    ) {
        systemSettingService.updateById(query);
        return R.ok();
    }

    @Operation(summary = "删除系统参数", description = "删除系统参数")
    @SaCheckPermission("platform:setting:delete")
    @DeleteMapping("{id}")
    public R<Object> deleteById(
            @Schema(description = "系统参数ID")
            @Min(value = 1) @NotNull
            @PathVariable Long id
    ) {
        systemSettingService.deleteById(id);
        return R.ok();
    }

    @Operation(summary = "批量删除系统参数", description = "批量删除系统参数")
    @SaCheckPermission("platform:setting:delete")
    @DeleteMapping
    public R<Object> deleteBatchByIdSet(
            @Schema(description = "系统参数ID列表")
            @NotEmpty @NotNull Set<Long> idList
    ) {
        systemSettingService.deleteBatchByIdSet(idList);
        return R.ok();
    }
}

